package com.example.testing.launchmode;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.testing.R;
/**
 *  singleTop:栈顶复用模式。在这种模式下，如果新的Activity已经处于任务栈的栈顶，那么此Activity就不会被重复创建了。
 *  同时，它的onNewIntent方法会被调用，通过此方法的参数，我们可以取出当前请求的信息。
 *  需要注意的是，这个Activity的onCreate、onStart不会被系统调用，因为它们并没有发生改变,但是onResume方法会在onNewIntent之后调用
 *  调用流程为：onPause->onNewIntent->onResume
 *  如果新Activity的实例已经存在，但是不在栈顶，那么新的Activity仍然会从新创建。
 */
public class SingleTopActivity extends Activity {
    private static final String TAG = "SingleTopActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e(TAG, "onCreate: ");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launchmode_singletop);

        findViewById(R.id.tv_launcher_mode).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SingleTopActivity.this, SingleTopActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onStart() {
        Log.e(TAG, "onStart: ");
        super.onStart();
    }

    /**
     * 不管当前Activity是否在栈顶，都会调用onResume方法
     * 如果已经在栈顶了，该方法在onNewIntent之后调用
     */
    @Override
    protected void onResume() {
        Log.e(TAG, "onResume: ");
        super.onResume();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        Log.e(TAG, "onNewIntent: ");
        super.onNewIntent(intent);
    }

    @Override
    protected void onRestart() {
        Log.e(TAG, "onRestart: ");
        super.onRestart();
    }

    @Override
    protected void onPause() {
        Log.e(TAG, "onPause: ");
        super.onPause();
    }

    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        Log.e(TAG, "onRestoreInstanceState:");
        String name = (String) savedInstanceState.get("name");
        Toast.makeText(this, "name:" + name, Toast.LENGTH_SHORT).show();
        super.onRestoreInstanceState(savedInstanceState);
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        Log.e(TAG, "onSaveInstanceState: ");
        super.onSaveInstanceState(outState);
        outState.putString("name", "josan");
    }

    @Override
    protected void onStop() {
        Log.e(TAG, "onStop: ");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.e(TAG, "onDestroy: ");
        super.onDestroy();
    }
}
